package com.zjgeport.zjgdanger.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;


import com.zjgeport.zjgdanger.R;

import java.util.ArrayList;
import java.util.HashMap;

public class MyImageAppAdapter extends BaseAdapter
{
	private Context main;

	private ArrayList<HashMap<String, Object>> list;

	private int width;

	public MyImageAppAdapter(Context main, ArrayList<HashMap<String, Object>> list,int wid)
	{
		this.main = main;
		this.list = list;
		this.width = wid;
	}

	public int getCount()
	{
		return list.size();
	}

	public Object getItem(int position)
	{
		return list.get(position);
	}

	public long getItemId(int position)
	{
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent)
	{
		VHolder vHolder = null;
		// 如果未选择或list列表为空时直接跳出，避免NullPointException
		if (position < 0 || list.size() <= 0)
		{
			return null;
		}
		// 如果为空重新导入ListView的布局文件，布局文件自定义
		if (convertView == null)
		{
			vHolder = new VHolder();
			convertView = LayoutInflater.from(main).inflate(R.layout.imageitem3, null);
			//LinearLayout ll = (LinearLayout) convertView.findViewById(R.id.ll_Name3);
			//convertView.setLayoutParams(new LinearLayout.LayoutParams(Math.abs(width/2),Math.abs(width/2)));
			convertView.setLayoutParams(new AbsListView.LayoutParams(Math.abs(width/3),Math.abs(width/3)));
			vHolder.textView  = (TextView) convertView.findViewById(R.id.txt_Name);
			vHolder.img  = (ImageView) convertView.findViewById(R.id.img_Name);
			convertView.setTag(vHolder);
		}else
		{
			vHolder = (VHolder) convertView.getTag();
		}

		//
		vHolder.img.setBackgroundResource((Integer) list.get(position).get("ItemImage"));
		vHolder.textView.setText(list.get(position).get("ItemText").toString());

		return convertView;
	}
	class VHolder{
		public TextView textView;
		public ImageView img;
	}
}
